package colibri;

import colibri.lcdui.Form;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;

/**
 * @author Bayron
 */
public class App {

    public static final int FIRE = GameCanvas.FIRE;
    public static final int UP = GameCanvas.UP;
    public static final int DOWN = GameCanvas.DOWN;
    public static final int LEFT = GameCanvas.LEFT;
    public static final int RIGHT = GameCanvas.RIGHT;
    public static final int PLATFORM_J2ME = 0;
    public static final int PLATFORM_BLACKBERRY = 1;
    public static int platform = PLATFORM_J2ME;
    public static boolean ISTOUCHSCREEN;
    public static String platformName;
    public static MIDlet midlet;
    private static Display display;
    public static int width = 0;
    public static int height = 0;
    public static int halfWidth = 0;
    public static int halfHeight = 0;
    public static String title = "";

    public static void setProperties(MIDlet midlet, String title) {
        platformName = System.getProperty("microedition.platform").toUpperCase();
        if (platformName.indexOf("RIM ") == -1) {
            platform = PLATFORM_J2ME;
        } else {
            platform = PLATFORM_BLACKBERRY;
        }
        App.midlet = midlet;
        App.title = title;
        display = Display.getDisplay(midlet);

        if (display != null && display.getCurrent() != null) {
            width = display.getCurrent().getWidth();
            height = display.getCurrent().getHeight();
            halfWidth = width / 2;
            halfHeight = height / 2;
        } else {
            Canvas canvas = new Canvas() {

                protected void paint(Graphics g) {
                }
            };
            canvas.setFullScreenMode(true);
            width = canvas.getWidth();
            height = canvas.getHeight();
            halfWidth = width / 2;
            halfHeight = height / 2;
            ISTOUCHSCREEN = canvas.hasPointerEvents();
        }
    }

    public static void setCurrent(Displayable nextDisplayable) {
        if (nextDisplayable == null) {
            throw new NullPointerException("Colibri error: Falha na classe App ao tentar exibir um Displayable no display. O agumento 'nextDisplayable' é nulo.");
        }
        display.setCurrent(nextDisplayable);
        if (nextDisplayable instanceof Canvas) {
            ISTOUCHSCREEN = ((Canvas) nextDisplayable).hasPointerEvents();
        }
        width = nextDisplayable.getWidth();
        height = nextDisplayable.getHeight();
        halfWidth = width / 2;
        halfHeight = height / 2;
    }

    public static void setCurrent(Alert alert, Displayable nextDisplayable) {
        if (alert == null) {
            throw new NullPointerException("Colibri error: Falha na classe App ao tentar exibir um Alert no display. O agumento 'alert' é nulo.");
        } else if (nextDisplayable == null) {
            throw new NullPointerException("Colibri error: Falha na classe App ao tentar exibir um Displayable no display. O agumento 'nextDisplayable' é nulo.");
        }
        display.setCurrent(alert, nextDisplayable);
        if (nextDisplayable instanceof Canvas) {
            ISTOUCHSCREEN = ((Canvas) nextDisplayable).hasPointerEvents();
        }
        width = nextDisplayable.getWidth();
        height = nextDisplayable.getHeight();
        halfWidth = width / 2;
        halfHeight = height / 2;
    }

    public static void alert(String msg) {
        Alert alert = new Alert(title, msg, null, AlertType.INFO);
        alert.setTimeout(3500);
        display.setCurrent(alert, Display.getDisplay(midlet).getCurrent());
    }

    public static void alert(String msg, Displayable nextDisplayable) {
        if (nextDisplayable == null) {
            throw new NullPointerException("Colibri error: Falha na classe App ao tentar exibir um Displayable no display. O agumento 'nextDisplayable' é nulo.");
        }
        Alert alert = new Alert(title, msg, null, AlertType.INFO);
        alert.setTimeout(3500);
        display.setCurrent(alert, nextDisplayable);
        if (nextDisplayable instanceof Canvas) {
            ISTOUCHSCREEN = ((Canvas) nextDisplayable).hasPointerEvents();
        }
        width = nextDisplayable.getWidth();
        height = nextDisplayable.getHeight();
        halfWidth = width / 2;
        halfHeight = height / 2;
    }

    public static void back() {
        if (display.getCurrent() == null || !(display.getCurrent() instanceof Form)) {
            return;
        }
        Displayable beforeDisplayable = ((Form)display.getCurrent()).getBeforeDisplayable();
        if (beforeDisplayable == null) {
            return;
        }
        App.setCurrent(beforeDisplayable);
        if (platform != PLATFORM_BLACKBERRY) {
            System.gc();
        }
    }

    public static void exit() {
        midlet.notifyDestroyed();
    }

    public static final boolean isSoftKey_LEFT(int keyCode) {
        if ((keyCode == -6 && platformName.indexOf("MOTOROLA") == -1) || //Nokia; SonyErricson; LG U8290; Samsung Devices; iDEN ic902
                keyCode == 21 || //Motorola Old keyCode
                keyCode == -21) {    //Motorola New keyCode; Panasonic VS3
            return true;
        } else {
            return false;
        }
    }

    public static final boolean isSoftKey_RIGHT(int keyCode) {
        if (keyCode == -7 || //Nokia; SonyErricson; LG U8290; Samsung Devices; iDEN ic902
                keyCode == 22 || //Motorola Old keyCode
                keyCode == -22) {   //Motorola New keyCode; Panasonic VS3
            return true;
        } else {
            return false;
        }
    }

    public final static boolean isKey_CLEAR(int keyCode) {
        switch (keyCode) {
            case 8:
                if (platform == PLATFORM_BLACKBERRY) { // BlackBerry
                    return true;
                } else {
                    return false;
                }
            case -8:
                if (platform == PLATFORM_J2ME) { // Nokia, HTC, SonyErricson, LG, LG, Samsung, etc
                    return true;
                } else {
                    return false;
                }
            default:
                if (getGameAction(keyCode) == App.LEFT && platform == PLATFORM_J2ME) {
                    return true;
                } else {
                    return false;
                }
        }
    }

    public final static int getGameAction(int keyCode) {
        Canvas canvas = new Canvas() {
            protected void paint(Graphics g) {
            }
        };
        try {
            return canvas.getGameAction(keyCode);
        } catch (IllegalArgumentException e) {
            return keyCode;
        }
    }
}
